﻿using Ruandao.Account;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Specialized;
using System.Web.Security;

namespace TestRuandao
{


    [TestClass()]
    public class RuandaoRoleProviderTest
    {
        static RuandaoRoleProvider target;
        
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            //target = new RuandaoRoleProvider();
            //string name = string.Empty;
            //NameValueCollection config = new NameValueCollection();
            //config.Add("name", "RuandaoAccount");
            //config.Add("title", "GroupSiteOwner");
            //config.Add("roleCategorySetter", "");
            //target.Initialize(name, config);

        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod()]
        public void RuandaoRoleProviderConstructorTest()
        {
            Assert.AreEqual("Ruandao.Account.RuandaoRoleProvider", System.Web.Security.Roles.Provider.GetType().FullName);
        }

        [TestMethod()]
        public void AddUsersToRolesTest()
        {
            string[] userNames = new string[] { "demo1", "demo2" };
            string[] roleNames = new string[] { "CreateNewRole" };
            target.AddUsersToRoles(userNames, roleNames);
        }

        [TestMethod()]
        public void CreateRoleTest()
        {
            string roleName = "CreateNewRole"; 
            target.CreateRole(roleName);
        }

        [TestMethod()]
        public void DeleteRoleTest()
        {
            string roleName = string.Empty; 
            bool throwOnPopulatedRole = false; 
            bool expected = false; 
            bool actual;
            actual = target.DeleteRole(roleName, throwOnPopulatedRole);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void FindUsersInRoleTest()
        {
            string roleName = "CreateNewRole"; 
            string userNameToMatch = string.Empty; 
            string[] expected = new string[] { "demo1", "demo2" }; 
            string[] actual;
            actual = target.FindUsersInRole(roleName, userNameToMatch);
            Assert.AreEqual("demo1", actual[0]);
            Assert.AreEqual("demo2", actual[1]);
        }

        [TestMethod()]
        public void GetAllRolesTest()
        {
            string[] actual;
            actual = target.GetAllRoles();
            Assert.AreEqual(1, actual.Length);
        }

        [TestMethod()]
        public void GetRolesForUserTest()
        {
            string userName = "demo1"; 
            string[] expected = new string[]{"CreateNewRole"}; 
            string[] actual;
            actual = target.GetRolesForUser(userName);
            Assert.AreEqual( "CreateNewRole", actual[0] );
        }

        [TestMethod()]
        public void GetUsersInRoleTest()
        {

            string roleName = "CreateNewRole";
            string[] expected = new string[] { "demo1", "demo2" };
            string[] actual;
            actual = target.GetUsersInRole(roleName);
            Assert.AreEqual("demo1", actual[0]);
            Assert.AreEqual("demo2", actual[1]);
        }

        [TestMethod()]
        public void InitializeTest()
        {
            Assert.AreEqual("Title(GroupSiteOwner),AccountContext(PublicUser)", target.ApplicationName, "RuandaoRoleProvider的Initialize失败。");
        }

        [TestMethod()]
        public void IsUserInRoleTest()
        {
            string userName = "demo1"; 
            string roleName = "CreateNewRole"; 
            bool expected = true; 
            bool actual;
            actual = target.IsUserInRole(userName, roleName);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void RemoveUsersFromRolesTest()
        {
            string[] userNames = new string[] {"demo1"};
            string[] roleNames = new string[] {"CreateNewRole"}; 
            target.RemoveUsersFromRoles(userNames, roleNames);
        }

        [TestMethod()]
        public void RoleExistsTest()
        {
            string roleName = "CreateNewRole";
            bool expected = true; 
            bool actual;
            actual = target.RoleExists(roleName);
            Assert.AreEqual(expected, actual);
        }
    }
}
